
package GUI;

import java.io.*;
import java.util.Vector;
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JFrame;
import java.awt.*;
import jess.*;
/**
 *
 * @author  Tony
 * @param <Batch>
 */
public class FramePreguntaTecnico1 extends javax.swing.JFrame {

    /** Creates new form FramePreguntaTecnico1 */
    private JFrame framePrincipal;
    private Vector vector;
    BufferedWriter writer;
    BufferedReader reader;
    Rete engine;
    Batch batch;
    
    public FramePreguntaTecnico1(JFrame fr) throws FileNotFoundException {
    	engine = new Rete ();
    	batch = new Batch();
        this.setVisible(true);
        this.framePrincipal=fr;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.//GEN-BEGIN:initComponents
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel_Idiomas = new javax.swing.JLabel();
        jLabel_Frances = new javax.swing.JLabel();
        jLabel_Ingles = new javax.swing.JLabel();
        jLabel_Aleman = new javax.swing.JLabel();
        jLabel_Experiencia = new javax.swing.JLabel();
        jLabel_Edad = new javax.swing.JLabel();
        jLabel_Curso = new javax.swing.JLabel();
        jComboBox_Curso = new javax.swing.JComboBox();
        jComboBox_Aleman = new javax.swing.JComboBox();
        jComboBox_Frances = new javax.swing.JComboBox();
        jComboBox_Ingles = new javax.swing.JComboBox();
        jButton_Asesorar = new javax.swing.JButton();
        jButton_Volver = new javax.swing.JButton();
        jComboBox_Edad = new javax.swing.JComboBox();
        jComboBox_Experiencia = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jLabel_Idiomas.setText("Idiomas:");
        jLabel_Idiomas.setAutoscrolls(true);

        jLabel_Frances.setText("- Francés");
        jLabel_Frances.setAutoscrolls(true);

        jLabel_Ingles.setText("- Inglés");
        jLabel_Ingles.setAutoscrolls(true);

        jLabel_Aleman.setText("- Alemán");
        jLabel_Aleman.setAutoscrolls(true);

        jLabel_Experiencia.setText("Experiencia en años");
        jLabel_Experiencia.setAutoscrolls(true);

        jLabel_Edad.setText("Edad");
        jLabel_Edad.setAutoscrolls(true);

        jLabel_Curso.setText("Curso");
        jLabel_Curso.setAutoscrolls(true);

        jComboBox_Curso.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Estudiante Carrera", "Carrera finalizada", "Estudiante Postgrado"}));
        jComboBox_Curso.setAutoscrolls(true);

        jComboBox_Aleman.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Alto", "Medio", "Bajo" }));
        jComboBox_Aleman.setAutoscrolls(true);

        jComboBox_Frances.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Alto", "Medio", "Bajo"}));
        jComboBox_Frances.setAutoscrolls(true);

        jComboBox_Ingles.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Alto", "Medio", "Bajo" }));
        jComboBox_Ingles.setAutoscrolls(true);

        jButton_Asesorar.setText("Asesorar");
        jButton_Asesorar.setAutoscrolls(true);
        jButton_Asesorar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
		try{
                jButton_AsesorarActionPerformed(evt);
			}
		catch(Exception ex){}
            }
        });

        jButton_Volver.setText("Volver");
        jButton_Volver.setAutoscrolls(true);
        jButton_Volver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                
			jButton_VolverActionPerformed(evt);
		}
        });

        String [] edad=new String[31];
        int j=0;
        for(int i=20;i<51;i++)
        {

            edad[j]=Integer.toString(i);
            j++;
        }
        jComboBox_Edad.setModel(new javax.swing.DefaultComboBoxModel(edad));

        String [] exper=new String[36];
        for(int i=0;i<36;i++)
        {
            exper[i]=Integer.toString(i);
        }
        jComboBox_Experiencia.setModel(new javax.swing.DefaultComboBoxModel(exper));
        jComboBox_Experiencia.setAutoscrolls(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel_Idiomas, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel_Frances, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                            .addComponent(jLabel_Ingles, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                            .addComponent(jLabel_Aleman, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE))
                        .addGap(20, 20, 20))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel_Experiencia, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                            .addComponent(jLabel_Edad, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                            .addComponent(jLabel_Curso, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE))
                        .addGap(60, 60, 60)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jComboBox_Experiencia, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jComboBox_Edad, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jComboBox_Curso, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jComboBox_Aleman, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jComboBox_Frances, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jComboBox_Ingles, 0, 85, Short.MAX_VALUE))
                .addGap(33, 33, 33)
                .addComponent(jButton_Asesorar, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                .addGap(20, 20, 20))
            .addGroup(layout.createSequentialGroup()
                .addGap(177, 177, 177)
                .addComponent(jButton_Volver, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE)
                .addContainerGap(249, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel_Edad)
                            .addComponent(jComboBox_Edad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel_Experiencia, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox_Experiencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel_Curso)
                            .addComponent(jComboBox_Curso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel_Idiomas)
                            .addComponent(jLabel_Ingles)
                            .addComponent(jComboBox_Ingles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel_Frances)
                            .addComponent(jComboBox_Frances, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel_Aleman)
                            .addComponent(jComboBox_Aleman, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(115, 115, 115)
                        .addComponent(jButton_Asesorar)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addComponent(jButton_Volver)
                .addContainerGap())
        );

        pack();//GEN-END:initComponents
    }// </editor-fold>                        

private void jButton_VolverActionPerformed(java.awt.event.ActionEvent evt) {                                                   
    this.setVisible(false);                                              
    this.framePrincipal.setVisible(true);                                                 
}                                              

private void jButton_AsesorarActionPerformed(java.awt.event.ActionEvent evt) throws IOException, JessException {                                                     
      

	
	
	
     int edad =Integer.parseInt((String) this.jComboBox_Edad.getSelectedItem());
     int experiencia=Integer.parseInt((String) this.jComboBox_Experiencia.getSelectedItem());
     String curso= (String) this.jComboBox_Curso.getSelectedItem();
     String ingles= (String) this.jComboBox_Ingles.getSelectedItem();
     String frances= (String) this.jComboBox_Frances.getSelectedItem();
     String aleman= (String) this.jComboBox_Aleman.getSelectedItem();
     
     //Pasar los parametros obtenidos, utilizar el motor de inferencia y
     //esperar los string correspondientes.

     ArrayList<String> idi= new ArrayList();
     idi.add(ingles);
     idi.add(aleman);
     idi.add(frances);
     inserta_idioma(idi);
     
     //Hechos segun la edad
    // Deffacts x = new Deffacts("edad joven");
     if ((edad>=20)&&(edad<=30))
    	 engine.assertString("(edad joven)");
     else if ((edad>=31)&&(edad<=40))
    	 engine.assertString("(edad media)");
     else if ((edad>=41)&&(edad<=50))
    	 engine.assertString("(edad avanzada)");
     //Hechos segun experiencia
     if (experiencia<4)
         engine.assertString("(tipoExp Poca)");
     else if ((experiencia>=4)&&(experiencia<10))
         engine.assertString("(tipoExp Media)");
     else if (experiencia>=10)
         engine.assertString("(tipoExp Alta)");
     //Hechos segun el estado del alumno
     if (curso.equals("Estudiante Carrera"))
         engine.assertString("(curso estudiante)");
     else if (curso.equals("Carrera finalizada"))
         engine.assertString("(curso acabado)");
     else if (curso.equals("Estudiante Postgrado"))
         engine.assertString("(curso postgrado)");     

        // Fact f = new Fact("vector", r);
    /* String curDir = System.getProperty("user.dir");
     System.out.println(curDir);*/
     //engine.batch("GUI/p2.clp");
     batch.batch("./GUI/p2.clp",engine);
     BufferedReader reader2= new BufferedReader(new InputStreamReader(new
     FileInputStream("C://salida.txt")));
     vector=new Vector();
     String linea;
     String Salario = "No lo se ";
     String jornada = "No lo se ";
     String empresa = "No lo se ";
     linea=reader2.readLine();
     Scanner s = new Scanner(linea);
     while (s.hasNext()) {
            String p = s.next(); // lee las palabras
            if(p.equals("(".concat("tipoEmpresa")))
                empresa=s.next();
            if(p.equals("("+"contrato"))
                jornada=s.next();
            if(p.equals("("+"salario"))
                Salario=s.next();
        }
     reader2.close();
     this.setVisible(false);      
     JFrame respuesta = new FrameRespuestaPregunta1(Salario.replace(')', ' '),jornada.replace(')', ' '),empresa.replace(')', ' '),this.framePrincipal);
     respuesta.setVisible(true);   
}                                                

public void inserta_idioma(ArrayList<String> x) throws JessException{
    int total =0;
    int i;
    for(i=0;i<x.size();i++){
        if ("Alto".equals(x.get(i)))
            total = total +2;
        else if ("Medio".equals(x.get(i)))
            total = total +1;
    }
    if (total<=2)
        engine.assertString("(idiomas ninguno)");
    else if (total>=5)
        engine.assertString("(idiomas alto)");
    else
        engine.assertString("(idiomas medio)");
}



    /**
    * @param args the command line arguments//GEN-BEGIN:variables
    */
  

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton_Asesorar;
    private javax.swing.JButton jButton_Volver;
    private javax.swing.JComboBox jComboBox_Aleman;
    private javax.swing.JComboBox jComboBox_Curso;
    private javax.swing.JComboBox jComboBox_Edad;
    private javax.swing.JComboBox jComboBox_Experiencia;
    private javax.swing.JComboBox jComboBox_Frances;
    private javax.swing.JComboBox jComboBox_Ingles;
    private javax.swing.JLabel jLabel_Aleman;
    private javax.swing.JLabel jLabel_Curso;
    private javax.swing.JLabel jLabel_Edad;
    private javax.swing.JLabel jLabel_Experiencia;
    private javax.swing.JLabel jLabel_Frances;
    private javax.swing.JLabel jLabel_Idiomas;
    private javax.swing.JLabel jLabel_Ingles;
    // End of variables declaration//GEN-END:variables

}
